/** 
 * File-Name:ArticleDao.java
 *
 * Created on 2011-6-2 下午03:45:50
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package site.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import neoutil.CommonDao;
import site.entity.Resource;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-2 下午03:45:50
 */
public class ResourceDao extends CommonDao<Resource, Long> {
	public ResourceDao(Connection connection) {
		super(Resource.class, connection);
	}

	public void save(Resource resource) throws SQLException {
		Object[] params = { resource.getUuid(), resource.getType(),
				resource.getNormcode(), resource.getTitle(),
				resource.getFormat(), resource.getCourse(),
				resource.getChapter(), resource.getChapters(),
				resource.getAuthor(), resource.getAuthorRealName(),
				resource.getPartners(), resource.getTeamCode(),
				resource.getProvider(), resource.getKeywords(),
				resource.getNote(), resource.getFileName(),
				resource.getPublicTime(), resource.getCreateTime(),
				resource.getRelate(), resource.getUpdateType(),
				resource.getAuthorId(), resource.getVisitNum(),
				resource.getStatus(), resource.getEntityDatetime(),
				resource.getEntityStatus(), resource.getCid() };
		execute(SQLString.SQL_RESOURCE_INSERT, params);
	}

	public void update(Resource resource) throws SQLException {
		System.out.println(resource.getTitle() + "\t" + resource.getUuid());
		Object[] params = { resource.getType(), resource.getNormcode(),
				resource.getTitle(), resource.getFormat(),
				resource.getCourse(), resource.getChapter(),
				resource.getChapters(), resource.getAuthor(),
				resource.getAuthorRealName(), resource.getPartners(),
				resource.getTeamCode(), resource.getProvider(),
				resource.getKeywords(), resource.getNote(),
				resource.getFileName(), resource.getPublicTime(),
				resource.getCreateTime(), resource.getRelate(),
				resource.getUpdateType(), resource.getAuthorId(),
				resource.getVisitNum(), resource.getStatus(), new Date(),
				resource.getEntityStatus(), resource.getCid(),
				resource.getUuid() };
		execute(SQLString.SQL_RESOURCE_UPDATE, params);
	}

	public void delete(Resource resource) throws SQLException {
		Object[] params = { resource.getUuid() };
		execute(SQLString.SQL_RESOURCE_DELETE, params);
	}

	public Resource getByNormcode(String normcode) {
		return findUnique("normcode", normcode);
	}

	public List<Resource> getByDate(Date date) {

		String sql = "SELECT * FROM resource WHERE entityDatetime>? AND entityDatetime<? AND entityStatus = 1";

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String start = sdf.format(date);
		String end = sdf.format(new Date(date.getTime() + 1000 * 60 * 60 * 24));

		return find(sql, new Object[] { start, end });
	}
}
